<template>
  <div>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item
          label="医院名称"
          prop="roleName"
        >
          <el-input
            v-model="queryParams.hospName"
            placeholder="请输入医院名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索</el-button>
          <el-button
            icon="el-icon-refresh"
            size="mini"
            @click="resetQuery"
          >重置</el-button>
        </el-form-item>
      </el-form>

      <el-row
        :gutter="10"
        class="mb8"
      >
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >批量删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        />
        <el-table-column
          label="医院名称"
          prop="hosname"
        />
        <el-table-column
          label="医院代码"
          prop="hoscode"
        />
        <el-table-column
          label="联系电话"
          prop="contactsPhone"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="联系人"
          prop="contactsName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="医院主页地址"
          prop="apiUrl"
        />
        <el-table-column
          label="状态"
          align="status"
          width="100"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="title"
      :visible.sync="isaddoredit"
    >
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item
          label="医院名称:"
          prop="hosname"
        >
          <el-input v-model="ruleForm.hosname"></el-input>
        </el-form-item>
        <el-form-item
          label="医院代码:"
          prop="hoscode"
        >
          <el-input v-model="ruleForm.hoscode"></el-input>
        </el-form-item>
        <el-form-item
          label="联系电话:"
          prop="contactsPhone"
        >
          <el-input v-model="ruleForm.contactsPhone"></el-input>
        </el-form-item>
        <el-form-item
          label="联系人:"
          prop="contactsName"
        >
          <el-input v-model="ruleForm.contactsName"></el-input>
        </el-form-item>
        <el-form-item
          label="医院主页:"
          prop="apiUrl"
        >
          <el-input v-model="ruleForm.apiUrl"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
          >提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import hospitalset from "@/api/hosp/hospitalset";
export default {
  data () {
    return {
      title: "",
      dataList: [],
      total: 0,
      loading: false,
      showSearch: true,
      queryParams: {},
      single: false,
      multiple: false,
      isaddoredit: false,
      ruleForm: {
        hosname: '',
        hoscode: '',
        contactsPhone: "",
        contactsName: "",
        apiUrl: ""
      },
      selectRow: []
    };
  },
  components: {
  },
  created () {
    this.getList()
  },
  methods: {
    handleQuery () {
      this.getList()
    },
    resetQuery () {
      console.log("重置");
    },
    getList () {
      let data = {
        "current": 0,
        "size": 10
      }
      hospitalset.list(data).then(response => {
        console.log("response:", response)
        this.dataList = response.result.records
        this.total = response.result.total
        this.loading = false;
      }
      );
    },
    handleStatusChange (row) {
      console.log("row", row)
      let data = {
        "id": row.id,
        "status": row.status
      }
      hospitalset.updateStatus(data).then(response => {
        console.log("切换状态response:", response)
      })
    },
    handleSelectionChange (row) {
      console.log("选择行:", row);
      this.selectRow = row
    },
    handleUpdate () {
      if (this.selectRow.length < 1) {
        this.$notify({
          title: '操作失败',
          message: '请选择一条记录修改',
          type: 'error'
        });
        return;
      }
      this.isaddoredit = true
      this.title = "修改医院设置"

      hospitalset.findById(this.selectRow[0].id).then(response => {
        console.log("医院设置信息", response)
        this.ruleForm = response.result
      })
    },
    handleAdd () {
      this.isaddoredit = true
      this.title = "新增医院设置"
      this.ruleForm = {}
    },
    handleExport () {
      this.$notify({
          title: '操作成功',
          message: '导出功能还在加紧开发中',
          type: 'success'
        });
    },
    handleDelete () {
      if (this.selectRow.length != 1 ) {
        this.$notify({
          title: '操作失败',
          message: '请选择一条记录删除',
          type: 'error'
        });
        return;
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        hospitalset.delById(this.selectRow[0].id).then(response => {
            console.log("操作成功", response)
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success'
            });
            this.getList()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = this.$refs[formName].model
          console.log(data)
          hospitalset.saveOrUpdate(this.ruleForm).then(response => {
            console.log("操作成功", response)

            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success'
            });

            this.isaddoredit = false
            this.getList()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm (formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
